package com.chixing.controller;

import com.chixing.pojo.Member;
import com.chixing.service.CouponService;
import com.chixing.service.CustomerService;
import com.chixing.service.PianhaoService;
import com.chixing.util.RSAUtil;
import com.chixing.util.ServerResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.security.PublicKey;

@Controller
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private PianhaoService pianhaoService;

    /**
     * 用户登陆
     *
     * @param memberTelno 电话号码
     * @param memberPwd   密码
     * @param session     session
     * @return 返回页面
     */
    @PostMapping("member/login")
    public String login(Long memberTelno, String memberPwd, HttpSession session)  {
        //AES加密后的密码是memberPwd
        System.out.println("电话号码：" + memberTelno);
        System.out.println("密码：" + memberPwd);

        //RSA公钥
        PublicKey publicKey = null;
        try {
            publicKey = RSAUtil.string2PublicKey(RSAUtil.PUBLIC_KEY_BASE64);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("公钥：" + publicKey);
//        PrivateKey privateKey = RSAUtil.string2PrivateKey(RSAUtil.PRIVATE_KEY_BASE64);
//        System.out.println("私钥：" + privateKey);

        //公钥加密AES的密文
        byte[] publicEncrypt = new byte[0];
        try {
            publicEncrypt = RSAUtil.publicEncrypt(memberPwd.getBytes(),publicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //公钥加密完的内容转Base64编码
        String publicEncryptStr = RSAUtil.byte2Base64(publicEncrypt);
        System.out.println("加密后的密码：" + publicEncryptStr);


        ServerResult serverResult = customerService.login(memberTelno, publicEncryptStr);

        if (serverResult.getCode() == 200) {
            Member member = (Member) serverResult.getData();
            member.setMemberAvatar(System.getProperty("catalina.home") + "/uplode/" + member.getMemberAvatar());
            System.out.println(member);
            session.setAttribute("customer", member);
            return "/customer/customer_info";
        } else {
            session.setAttribute("login","用户名或密码出错");
            return "redirect:/customer/login.jsp";
        }
    }

    /**
     * 退出登录
     *
     * @param session 删除session
     * @return 返回首页
     */
    @GetMapping("member/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("customer");
        return "redirect:/index.jsp";
    }

    @PostMapping("member/regist")
    public String regist(Member member, HttpSession session) {
        System.out.println(member);
        System.out.println("AES密码是：" + member.getMemberPwd());


        //RSA公钥
        PublicKey publicKey = null;
        try {
            publicKey = RSAUtil.string2PublicKey(RSAUtil.PUBLIC_KEY_BASE64);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("公钥：" + publicKey);
//        PrivateKey privateKey = RSAUtil.string2PrivateKey(RSAUtil.PRIVATE_KEY_BASE64);
//        System.out.println("私钥：" + privateKey);

        //公钥加密AES的密文
        byte[] publicEncrypt = new byte[0];
        try {
            publicEncrypt = RSAUtil.publicEncrypt(member.getMemberPwd().getBytes(),publicKey);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //公钥加密完的内容转Base64编码
        String publicEncryptStr = RSAUtil.byte2Base64(publicEncrypt);
        System.out.println("加密后的密码：" + publicEncryptStr);
        member.setMemberPwd(publicEncryptStr);

        ServerResult serverResult = customerService.regist(member);

        if (serverResult.getData().equals("该电话号码已注册") && serverResult.getCode() == 201) {
            session.setAttribute("regist",serverResult.getData());
            return "redirect:/customer/regist.jsp";
        }


        if (serverResult.getCode() == 200) {

            couponService.receiveCoupon(80002,member.getMemberId());

            pianhaoService.pianhao2Update(member.getMemberId(),30001,3.0f);

            System.out.println("成功注册");
            session.setAttribute("customer", member);
            return "/index";
        }

        return "redirect:/customer/regist";
    }

    @GetMapping("member/deleteUser")
    public ModelAndView deleteUser(HttpSession session) {

        Member member = (Member) session.getAttribute("customer");

        member.setMemberStatus(1);

        ServerResult serverResult = customerService.deleteUser(member);

        ModelAndView mav = new ModelAndView();

        if (serverResult.getCode() == 200) {
            session.removeAttribute("customer");
            mav.setViewName("index");
        } else {
            System.out.println("删除用户失败");
            mav.setViewName("customer/customer_info");
        }

        return mav;


    }

    @PostMapping("member/update")
    public String update(Member member, HttpSession session) {

        ServerResult serverResult = customerService.updateCustomer(member);

//        System.out.println(serverResult.getData());

        if (serverResult.getCode() == 200) {
            System.out.println(member);
            member.setMemberAvatar(System.getProperty("catalina.home") + "/uplode/" + member.getMemberAvatar());
            session.setAttribute("customer", member);
            return "customer/customer_info";
        } else
            return "redirect:customer/customer_info";
    }

    @PostMapping("member/upload")
    public String uploadUserImg(@RequestParam("file") MultipartFile file, HttpSession session) {

        //获取用户上传的图片的路径
        String[] stringList = customerService.saveFile(file);

        Member member = (Member) session.getAttribute("customer");
        //将图片名称保存到数据库中
        member.setMemberAvatar(stringList[1]);

        System.out.println("传给后端：" + member);

        ServerResult serverResult = customerService.updateCustomer(member);

        //数据库保存完图片名称后，将图片全路径传给前端
        member.setMemberAvatar(stringList[0]);

        if (serverResult.getCode() == 200) {
            System.out.println("传给前端的member：" + member);
            session.setAttribute("customer", member);
            return "customer/customer_info";
        }
        return "redirect:customer/customer_info";
    }

}
